package fr.maistre.champagne;

import fr.maistre.SystemeSauvegarde.SaveGameManager;
import fr.maistre.champagne.fragments.CreateGameFragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends FragmentActivity {
	
	private SaveGameManager saveManager;
	private Context context = this;
	Button buttonContinue;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		saveManager = new SaveGameManager(this.getApplicationContext());

		Button newGameButton = (Button) findViewById(R.id.buttonNewGame);
		newGameButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				DialogFragment createGameFragment = new CreateGameFragment();
				createGameFragment
						.show(getSupportFragmentManager(), "New Game");
			}
		});
		
		buttonContinue = (Button)findViewById(R.id.buttonContinue);
		
		buttonContinue.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent game = new Intent(context , GameActivity.class);
				game.putExtra("NewGame", false);
				startActivity(game);
				
			}
		});
	}
 @Override
protected void onResume() {
	 if(this.saveManager.saveExist()){
			this.buttonContinue.setClickable(true);
			this.buttonContinue.setVisibility(View.VISIBLE);
		}else{
			this.buttonContinue.setClickable(false);
			this.buttonContinue.setVisibility(View.GONE);
		}
	super.onResume();
}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
